package com.mvc.web.common.md5Util;

import java.security.MessageDigest;  
  
/**
* Description: 加密工具�?
* All Rights Reserved.
* @version 1.0  2015-12-30 上午11:42:13 by 白海安（ha.bai@zuche.com�?
*/
public final class MD5Util {  
	private static final int MAGIC_NUMBER = 0xff;
	
	private MD5Util(){};
  
    /*** 
     * MD5加码 生成32位md5�?
     */  
    public static String string2MD5(String inStr){  
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            System.out.println(e.toString());  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  {
        	byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & MAGIC_NUMBER;  
            if (val < 2*2*2*2){
            	hexValue.append("0");  
            }
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
    }  
  
    /** 
     * 加密解密算法 执行�?��加密，两次解�?
     */   
//    public static String convertMD5(String inStr){  
//        char[] a = inStr.toCharArray();  
//        for (int i = 0; i < a.length; i++){  
//            a[i] = (char) (a[i] ^ 't');  
//        }  
//        String s = new String(a);  
//        return s;  
//    }  
//  
    public static void main(String[] args) {  
        String s = new String("admin");  
        System.out.println("MD5后：" + string2MD5(s));  
    }  


}  
